import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from "../components/Login"
const Login = () => import(/* webpackChunkName: "group-foo" */ '../components/Login')
// import Home from "../views/Home"
const Home = () => import(/* webpackChunkName: "group-foo" */ '../views/Home')
// import Users from "../views/Users"
const Users = () => import(/* webpackChunkName: "group-foo" */ '../views/Users')
// import Rights from "../views/power/Rights"
const Rights = () => import(/* webpackChunkName: "group-foo" */ '../views/power/Rights')
// import Roles from "../views/power/Roles"
const Roles = () => import(/* webpackChunkName: "group-foo" */ '../views/power/Roles')
// import Goods from "../views/goods/Goods"
const Goods = () => import(/* webpackChunkName: "group-foo" */ '../views/goods/Goods')
// import Params from "../views/goods/Params"
const Params = () => import(/* webpackChunkName: "group-foo" */ '../views/goods/Params')
// import Categories from "../views/goods/Categories"
const Categories = () => import(/* webpackChunkName: "group-foo" */ '../views/goods/Categories')
// import Welcome from "../components/Welcome"
const Welcome = () => import(/* webpackChunkName: "group-foo" */ '../components/Welcome')
// import Add from "../views/goods/Add"
const Add = () => import(/* webpackChunkName: "group-foo" */ '../views/goods/Add')
// import Order from '../views/order/Order'
const Order = () => import(/* webpackChunkName: "group-foo" */ '../views/order/Order')
// import Report from '../views/reports/Report'
const Report = () => import(/* webpackChunkName: "group-foo" */ '../views/reports/Report')


Vue.use(VueRouter)

  const routes = [
    {
      path:"/",
      redirect:"/login"
    },
    {
    path:"/login",
    component:Login,
    name:"login",
    },
    
    {
      path:"/home",
      component:Home,
      name:"home",
      redirect:'/welcome',
      children: [
        {
          path: "/welcome",
          name: 'Welcome',
          component: Welcome
        },
        {
          path:"/users",
          name:"users",
          component:Users,
        },
        {
          path:"/rights",
          name:"rights",
          component:Rights,
        },
        {
          path:"/roles",
          name:"roles",
          component:Roles,
        },
        {
          path:"/goods",
          name:"goods",
          component:Goods,
        },
        {
          path:"/goods/add",
          name:"add",
          component:Add,
        },
        {
          path:"/params",
          name:"params",
          component:Params,
        },
        {
          path:"/categories",
          name:"categories",
          component:Categories,
        },
        {
          path:"/orders",
          name:"order",
          component:Order,
        },
        {
          path:"/reports",
          name:"report",
          component:Report,
        },
       
      ]
    },
    
  
]
// 解决ElementUI导航栏重复点菜单报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}


const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,from,next)=>{
  if (to.path==="/login") {
    next();
  }
  const token = window.sessionStorage.getItem("token");
  // 没有token的话,跳转到login
  if (!token) {
    next("/login");
  }
  // 有token,代表已经登录
  next();
})

export default router;
